home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Interactive Reference Guide
/
C-C++ Interactive Reference Guide.iso
/
c_ref
/
csource5
/
376_01
/
os2tool.000
/
DIRSCAN.C
< prev
next >
Wrap
C/C++ Source or Header
|
1992-07-04
|
7KB
|
293 lines
/*
* DIRSCAN.C - General directory scanner function.
*
*
* PROGRAMMER: Martti Ylikoski
* CREATED: 30.10.1991
* VERSION: 1.0
*
*/
//#undef MSDOS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <direct.h>
#include <errno.h>
#ifdef MSDOS
#include <dos.h>
#include <errno.h>
#define TRUE 1
#define FALSE 0
#endif
#ifndef MSDOS
#include <os2.h>
#endif
#include "dirscan.h"
/*==================================================
*
* Search for a file.
*
*=================================================*/
#ifdef MSDOS
int DirScan( int fcount, char *fname[], unsigned findattr, int no_default, int (*DirInitFn) (char *),
int (*DirExitFn)(char *),int (* FileHandlerFn)(char *), int DownCnt, int sc_flags )
{
int status, j, k, ret ;
char curdir[80], buf[100], sysbuf[80], lastdir[80] ;
int curdrive ;
int i ;
struct find_t findbuf ;
// curdrive = _getdrive() ;
getcwd(curdir, (int) sizeof(curdir)) ;
// buf[0] = 'A' + curdrive -1 ;
// strcpy(&buf[1], ":\\") ;
// strcat(buf,curdir) ;
strcpy(buf,curdir) ;
// if (curdirlen != 0) /* not in the main directory */
if (curdir[strlen(curdir) -1] != '\\')
strcat(buf,"\\") ;
if (DirInitFn != NULL)
DirInitFn(buf) ;
for(j = 0 ; j < fcount ; j++) /* scan through all file specifiers */
{
if (( status = _dos_findfirst(fname[j], findattr, &findbuf)) == 0)
{
if ( no_default == FALSE || (no_default == TRUE && (findattr & findbuf.attrib) != 0))
{
strcpy(sysbuf, buf) ;
strcat(sysbuf,findbuf.name) ;
if ((ret = FileHandlerFn(sysbuf)) != 0)
return( ret ) ;
}
do
{
if (( status = _dos_findnext(&findbuf)) == 0)
{
if ( no_default == FALSE || (no_default == TRUE && (findattr & findbuf.attrib) != 0))
{
strcpy(sysbuf, buf) ;
strcat(sysbuf,findbuf.name) ;
if ((ret = FileHandlerFn(sysbuf)) != 0)
return( ret ) ;
}
}
} while (status == 0) ;
}
} /* all file specifiers */
/* span to subdirectories */
if (DownCnt == 0)
{
if (DirExitFn != NULL)
DirExitFn(buf) ;
return( 0 ) ;
}
if (sc_flags & SC_BEFORESUBDIRS && DirExitFn != NULL)
DirExitFn(buf) ;
DownCnt -- ;
if (( status = _dos_findfirst("*.*", _A_SUBDIR, &findbuf)) != 0)
{
return( 0 ) ; /* no subdirectory */
}
if ( findbuf.name[0] != '.' &&
(findbuf.attrib & _A_SUBDIR)) /* only for root */
{
chdir (findbuf.name) ;
if ((ret = DirScan(fcount, &fname[0], findattr, no_default, DirInitFn, DirExitFn, FileHandlerFn, DownCnt, sc_flags)) != 0)
return( 1 ) ;
chdir("..") ;
}
do
{
if (( status = _dos_findnext(&findbuf)) == 0)
{
if ( findbuf.name[0] != '.' &&
(findbuf.attrib & _A_SUBDIR))
{
chdir (findbuf.name) ;
if ((ret = DirScan(fcount, &fname[0], findattr, no_default, DirInitFn, DirExitFn, FileHandlerFn, DownCnt, sc_flags)) != 0)
return( 1 ) ;
chdir("..") ;
}
}
} while (status == 0) ;
if (sc_flags & SC_AFTERSUBDIRS && DirExitFn != NULL)
DirExitFn(buf) ;
return( 0 ) ;
}
#endif
#ifndef MSDOS
int DirScan( int fcount, char *fname[], USHORT findattr, int no_default, int (*DirInitFn) (char *),
int (*DirExitFn)(char *),int (* FileHandlerFn)(char *), int DownCnt, int sc_flags )
{
int status, j, k, ret ;
char curdir[80], buf[100], sysbuf[80] ;
USHORT curdirlen ;
USHORT curdrive, i ;
ULONG tmp ;
HDIR hdir;
USHORT SearchCount;
FILEFINDBUF findbuf[1];
DosQCurDisk (&curdrive, &tmp) ;
curdirlen = sizeof(curdir) ;
DosQCurDir(curdrive, curdir, &curdirlen ) ;
buf[0] = 'A' + curdrive -1 ;
strcpy(&buf[1], ":\\") ;
strcat(buf,curdir) ;
// if (curdirlen != 0) /* not in the main directory */
if (curdir[0] != '\0')
strcat(buf,"\\") ;
if (DirInitFn != NULL)
DirInitFn(buf) ;
for(j = 0 ; j < fcount ; j++) /* scan through all file specifiers */
{
SearchCount = 1 ; //(int) sizeof(findbuf)/sizeof(findbuf[0]) ;
hdir = HDIR_CREATE;
/* search for files */
if ( (status = DosFindFirst2(fname[j],
&hdir,
findattr,
&findbuf[0],
sizeof(findbuf),
&SearchCount,
FIL_STANDARD,
0L)) == 0)
{
for (i = 0 ; i < SearchCount; i++)
{
if ( no_default == FALSE || (no_default == TRUE && (findattr & findbuf[i].attrFile) != 0))
{
strcpy(sysbuf, buf) ;
strcat(sysbuf,findbuf[i].achName) ;
if ((ret = FileHandlerFn(sysbuf)) != 0)
{
DosFindClose(hdir) ;
return( ret ) ;
}
}
}
do
{
if ((status = DosFindNext(hdir,
&findbuf[0],
sizeof(findbuf),
&SearchCount)) == 0)
{
for (i = 0 ; i < SearchCount; i++)
{
if ( no_default == FALSE || (no_default == TRUE && (findattr & findbuf[i].attrFile) != 0))
{
strcpy(sysbuf, buf) ;
strcat(sysbuf,findbuf[i].achName) ;
if ((ret = FileHandlerFn(sysbuf)) != 0)
{
DosFindClose(hdir) ;
return( ret ) ;
}
}
}
}
} while (status == 0) ;
}
DosFindClose(hdir) ;
} /* all file specifiers */
/* span to subdirectories */
if (DownCnt == 0)
{
if (DirExitFn != NULL)
DirExitFn(buf) ;
return( 0 ) ;
}
if (sc_flags & SC_BEFORESUBDIRS && DirExitFn != NULL)
DirExitFn(buf) ;
DownCnt -- ;
hdir = HDIR_CREATE;
SearchCount = 1 ; // (int) sizeof(findbuf)/sizeof(findbuf[0]) ;
if ( (status = DosFindFirst2("*.*",
&hdir,
FILE_DIRECTORY,
&findbuf[0],
sizeof(findbuf),
&SearchCount,
FIL_STANDARD,
0L)) != 0)
{
DosFindClose(hdir) ;
return( 0 ) ; /* no subdirectory */
}
for( i = 0; i < SearchCount; i++)
{
if ( findbuf[i].achName[0] != '.' &&
(findbuf[i].attrFile & FILE_DIRECTORY) ) /* only for root */
{
chdir (findbuf[i].achName) ;
if ((ret = DirScan(fcount, &fname[0], findattr, no_default, DirInitFn, DirExitFn, FileHandlerFn, DownCnt, sc_flags)) != 0)
{
DosFindClose(hdir) ;
return( 1 ) ;
}
chdir("..") ;
}
}
do
{
if ((status = DosFindNext(hdir,
&findbuf[0],
sizeof(findbuf),
&SearchCount)) == 0)
{
for (i=0 ; i < SearchCount; i++)
{
if ( findbuf[i].achName[0] != '.' &&
(findbuf[i].attrFile & FILE_DIRECTORY))
{
chdir (findbuf[i].achName) ;
if(( ret = DirScan(fcount, &fname[0], findattr, no_default, DirInitFn, DirExitFn, FileHandlerFn, DownCnt, sc_flags)) != 0)
{
DosFindClose(hdir) ;
return( 1 ) ;
}
chdir("..") ;
}
}
}
} while (status == 0) ;
DosFindClose(hdir) ;
if (sc_flags & SC_AFTERSUBDIRS && DirExitFn != NULL)
DirExitFn(buf) ;
return( 0 ) ;
}
#endif